<template>
  <div>
    <!-- 渲染角色列表 -->
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in rolesList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="confirmHandle">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getRolesAPI } from '@/api/setting'
import { getUserDetailInfo } from '@/api/user'
import { setRoleAPI } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    id: {
      type: [Number, String],
      required: true
    }
  },
  data() {
    return {
      checkList: [], // 选中复选框的值
      rolesList: [], // 复选框显示的值
      employeeDetail: {}// 员工详情
    }
  },
  created() {
    this.getRoles()
    this.getUserDetailInfo()
  },
  methods: {
    // 获取角色列表
    async getRoles() {
      const res = await getRolesAPI()
      this.rolesList = res.data.rows
    },
    // 获取当前员工的详情信息
    async  getUserDetailInfo() {
      const res = await getUserDetailInfo(this.id)
      this.checkList = res.data.roleIds
      this.employeeDetail = res.data
    },
    // 点击取消按钮,触发事件
    cancleDialog() {
      this.$emit('closeDialog')
    },
    // 点击确定按钮,触发事件
    async confirmHandle() {
      await setRoleAPI({ id: this.id, roleIds: this.checkList })
      this.$emit('closeDialog')
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  margin-top: 30px;
}
</style>
